//
//  FavoriteShopsTableViewController.m
//  Sydeny
//
//  Created by Rainbow on 8/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FavoriteShopsTableViewController.h"
#import "PersistenceData.h"

@implementation FavoriteShopsTableViewController

- (void)dealloc
{
    [editBtn release];
    editBtn = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad]; 
    
    self.navigationItem.title = @"我的收藏";
    
    loadFinish = YES;
    [self.shops addObjectsFromArray:[[PersistenceData sharedData] getAllShops]];
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    
    editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [editBtn setBackgroundImage:[UIImage imageNamed:@"right_btn.png"] forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(tapEditBtn) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *rightItem = [[[UIBarButtonItem alloc] initWithCustomView:editBtn] autorelease];
    self.navigationItem.rightBarButtonItem = rightItem;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [self.shops count]) {
        return NO;
    }
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (editingStyle == UITableViewCellEditingStyleDelete) {
        Shop *shop = [self.shops objectAtIndex:indexPath.row];
		[[PersistenceData sharedData] deleteShop:shop.shopId];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:[[PersistenceData sharedData] getAllShops]];
        [self.tableView reloadData];
	}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tapEditBtn
{
    if (self.tableView.editing) {
        self.tableView.editing = NO;
        [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    }
    else{
        self.tableView.editing = YES;
        [editBtn setTitle:@"完成" forState:UIControlStateNormal];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


@end
